package com.hbzhit.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbzhit.common.utils.PageUtils;
import com.hbzhit.common.utils.Query;
import com.hbzhit.common.utils.ShiroUtils;
import com.hbzhit.modules.sys.dao.SysRoleDao;
import com.hbzhit.modules.sys.entity.SysRoleEntity;
import com.hbzhit.modules.sys.service.SysRoleMenuService;
import com.hbzhit.modules.sys.service.SysRoleOrgService;
import com.hbzhit.modules.sys.service.SysRoleService;
import com.hbzhit.modules.sys.service.SysUserRoleService;
import lombok.AllArgsConstructor;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 角色
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service("sysRoleService")
@AllArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRoleEntity> implements SysRoleService {
    private final SysRoleMenuService sysRoleMenuService;
    private final SysRoleOrgService sysRoleOrgService;
    private final SysUserRoleService sysUserRoleService;
	private final IdentityService identityService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String roleName = (String)params.get("roleName");
		Long createUserId = (Long)params.get("createUserId");

		IPage<SysRoleEntity> page = this.page(
			new Query<SysRoleEntity>().getPage(params),
			new QueryWrapper<SysRoleEntity>()
				.like(StringUtils.isNotBlank(roleName),"role_name", roleName)
				.eq(createUserId != null,"create_user_id", createUserId)
		);

		return new PageUtils<>(page);
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRole(SysRoleEntity role) {
        role.setCreateUserId(ShiroUtils.getUserId());
        role.setCreateTime(new Date());
        this.save(role);
        this.linkSave(role);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysRoleEntity role) {
        this.updateById(role);
        this.linkSave(role);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] roleIds) {
        //删除角色
        this.removeByIds(Arrays.asList(roleIds));

        //删除角色与菜单关联
        sysRoleMenuService.deleteBatch(roleIds);

        //删除角色与组织关联
        sysRoleOrgService.deleteBatch(roleIds);

        //删除角色与用户关联
        sysUserRoleService.deleteBatch(roleIds);
    }


    @Override
	public List<Long> queryRoleIdList(Long createUserId) {
		return baseMapper.queryRoleIdList(createUserId);
	}

    /**
     * 角色关联保存
     * @param role 角色
     */
    private void linkSave(SysRoleEntity role) {
        //保存角色与菜单关系
        if (role.getMenuIdList() != null) {
            sysRoleMenuService.saveOrUpdate(role.getRoleId(), role.getMenuIdList());
        }

        //保存角色与组织关系
        if (role.getOrgIdList() != null) {
            sysRoleOrgService.saveOrUpdate(role.getRoleId(), role.getRoleOrgType(), role.getOrgIdList());
        }

        //保存角色与用户关系
        if (role.getUserIdList() != null) {
            sysUserRoleService.saveOrUpdateRole(role.getRoleId(), role.getUserIdList());
        }

        //同步流程引擎角色
        identityService.deleteGroup(String.valueOf(role.getRoleId()));
        Group adminGroup=identityService.newGroup(String.valueOf(role.getRoleId()));
        adminGroup.setName(role.getRoleName());
        identityService.saveGroup(adminGroup);
    }

}
